var appPath = getAppPath();

$(function(){
	initJspGrid();
	initUploader();
});

// 特殊模板组 列表数据初始化
function initJspGrid(){
	$('#tmplSetGrid').datagrid({
	    url:appPath + "/tmpSet/getTmplSetInfoPg.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'thumb',title:'缩略图',width:60,align:'center',sortable:false,
	        	formatter:function(thumb, rowData, rowIndex){
	        		var retUrl = '';
	        		if (rowData.thumb) {
	        			retUrl = '<a href="' + rowData.thumb + '" target="_blank">'
        				+ '<div style="display:table;text-align:center;height:120px;width:100%">'
        				+ '<span style="display:table-cell;vertical-align:middle">'
        				+ '<img src="'+thumb+'?x-oss-process=image/resize,m_lfit,h_100,w_125'
        				+ '" style="display:inline;max-height:100px;max-width:125px"></span></div></a>';
	        		}
	        		return retUrl;
	        	}},
			{field:'edit',title:'编辑',width:30,align:'center',sortable:false,
				formatter:function(value,rowData,rowIndex){
					var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑' ";
					ret += " onClick='editTmpl("+rowData.ts_id+")'/></a>";
					return ret;
				}
			},
	        {field:'ts_id',title:'模板组ID',width:30,align:'center',sortable:false},
	        {field:'use',title:'用途',width:40,align:'center',sortable:false,
		    	  formatter:function(use, rowData, rowIndex){
		    		  var u = '<span style="color:red">未知</span>';
		    		  if(use>=1 && use<=99){
		    			  u = '<span 通用style="color:red">通用</span>';
		    		  }else if(use>=101 && use<=199){
		    			  u = '<span style="color:blue">App用</span>';
		    		  }else if(use>=201 && use<=299){
		    			  u = '<span style="color:blue">网页端用</span>';
		    		  }else if(use>=301 && use<=399){
		    			  u = '<span style="color:#8470FF;">微信小程序</span>';
		    		  }
		    		  return u;
		    	  }
		      },
	        {field:'title',title:'编组名称',width:70,align:'left',sortable:false},
	        {field:'desc',title:'用途简介',width:100,align:'left',sortable:false,
	        	formatter:function(desc, rowData, rowIdx){
	        		var ret = rowData.desc;
	        		if(ret && ret.length>10){
	        			return ret.substr(0,10)+'...';
	        		}
	        		return ret;
	        	}
	        },
	        {field:'order',title:'排序',width:20,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(!rowData.ts_id) return "";
		    		  return "<a href='javascript:void(0)' onClick='editOrder("+rowData.ts_id+","+rowData.order+")'>"+rowData.order+"</a>";
		    	  }
		    },
	        {field:'publish',title:'发布状态',width:35,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<spna style="color:green;">已发布</span>';
	        		return '<span style="color:red;">未发布</span>';
	        	}
	        },
	        {field:'design', title:'内容', width:40, align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		return "<a href='javascript:void(0)' onclick='design(\""+rowData.title+"\","+rowData.ts_id+")'>点击设置</a>";
	        	}
	        },
	        {field:'start_time',title:'起始有效时间',width:50,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	  		     	if(value){
	  		     		var show = value.substr(0,19);
	  		     		if(rowData.valid==10) {// 未开始
	  		     			show = '<span style="color:green">'+show+'</span>'
	  		     		}
	  		     		return show;
		  		  	}
	        	}
	        },
	        {field:'end_time',title:'截止有效时间',width:50,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	  		     	if(value){
	  		     		var show = value.substr(0,19);
	  		     		if(rowData.valid==30) {//已结束
	  		     			show = '<span style="color:red">'+show+'</span>'
	  		     		}
	  		     		return show;
		  		  	}
	        	}
	        },
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:50,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[{
				text:'添加模板组',
				iconCls:'icon-add',
				handler:function(){
					addTmplG();
				}
			},
			{
				text:'发布模板组',
				iconCls:'icon-ok',
				handler:function(){
					doPublish();
				}
			},
			{
				text:'取消发布模板组',
				iconCls:'icon-cancel',
				handler:function(){
					cancelPublish();
				}
			},
			{
				text:'删除模板组',
				iconCls:'icon-cancel',
				handler:function(){
					delTmplGp();
				}
			}]
	});
}

//展示模板组下---设计模板列表
function design(title,id){
	$("#ts_id").val(id);
	$("#tsId").html(id);
	$("#stTitle").html(title);
	$('#tDesignDlg').window({
		title:'模板组:模板列表',
		iconCls:'icon-add',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#tDesignDlg').window('open');
	$('#tDesignDlg').window('move',{top:20,left:20});
	initDesignGrid();
}

function initUploader(){
	var url = "http://upload.itbour.com/upload/user?device=101&user_type=0&user_id="+(g_userId||0);
    var option = {};
    option.id = 'selImgFile';
    option.url = url;
    option.callback = function(res){
    	$("#tmpImg").html('');
    	if(!res.success){
    		alert(res.msg);
            return;
    	}
    	if(res && res.data && res.data.url){
    		var showImg = '';
			if(res.data.url && res.data.url.length>0) {
				showImg = res.data.url.replace('oss-cn-hangzhou.aliyuncs.com','img-cn-hangzhou.aliyuncs.com')
					+'@200h_200w_1l.png';
			}
			var imgHtml = '<img src="'+showImg+'" style="display:inline;max-height:100px;max-width:200px">';
			$("#tmpImg").append(imgHtml).val(res.data.url||'');
    	}
    }
    option.imgExts = ['jpg','jpeg','png'];
    //option.bindInputFileName ='fileNameInput';
    var uploader = new AjaxUploadEx(option);
}


/* * Click to open the add template group page
 */
function addTmplG(){
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#tmpImg").html('').val('');
	$("#editTmplUse").combobox('select',301).combobox('enable');
	$("#editBtn").show();
	$('#editDlg').window({
		title:'新建模板组',
		iconCls:'icon-add',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}

/*
 * Click to open the edit template group list
 */
function editTmpl(tsId){
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#tmpImg").html('').val('');
	$("#editTsid").val(tsId);
	
	var url = appPath + "/tmpSet/getTmplSetLst.action?ts_id="+ tsId;
	$.get(url,function(ret){
		if (ret && ret.length>0){
			var info = ret[0];
			$("#editTmplTitle").val(info.title||'');
			$("#editTmplDesc").val(info.desc||'');
			$("#DateStart").datebox('setValue',info.start_time);
			$("#DateEnd").datebox('setValue',info.end_time);
			$("#editTmplUse").combobox('select',info.use||2).combobox('disable');
			var showImg = '';
			if(info.thumb && info.thumb.length>0) {
				showImg = info.thumb.replace('oss-cn-hangzhou.aliyuncs.com','img-cn-hangzhou.aliyuncs.com')
					+'@200h_200w_1l.png';
			}
			var imgHtml = '<img src="'+showImg+'" style="display:inline;max-height:100px;max-width:200px">';
			$("#tmpImg").append(imgHtml).val(info.thumb||'');
		}
	});
	
	$("#editBtn").show();
	$('#editDlg').window({
		title:'编辑模板组',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}

function cancelEdit(){
	$('#editDlg').window('close');
}

/*
 * Submit the edit template group
 */
function doEdit(){
	var tsId = $("#editTsid").val();
	var use = $("#editTmplUse").combobox('getValue');
	if(use==''||use.length<=0){
		$.messager.alert('提示信息','用途不能为空','info');
		return;
	}
	var title = $("#editTmplTitle").val();
	var desc = $("#editTmplDesc").val();
	var imgUrl = $("#tmpImg").val();
	var findDtS = $("#DateStart").datebox('getValue');
	var findDtE = $("#DateEnd").datebox('getValue');
	var cFindDtS = chkInputDate(findDtS);
	var eFindDtS = chkInputDate(findDtE);
	if(cFindDtS.length<=0){
		$.messager.alert('提示信息','起始时间格式不正确','info');
		return;
	}
	if(eFindDtS.length<=0){
		$.messager.alert('提示信息','截止时间格式不正确','info');
		return;
	}
	if(cFindDtS>eFindDtS){
		$.messager.alert('','起始日期不能大于截止日期！','info');
		return;
	}
	
	if(title && title.length>0) {
		var url = appPath+"/itbTmplSet/addTmplSet.action"
		var sendData = {
			"tmplSetDto.ts_id": tsId||"",
			"tmplSetDto.use": use,
			"tmplSetDto.title": title || "",
			"tmplSetDto.desc": desc || "",
			"tmplSetDto.thumb":imgUrl||"",
			"tmplSetDto.start_time":findDtS||"",
			"tmplSetDto.end_time":findDtE||""
		};
	    $.post(url, sendData, function(data){  
			if (data == "ok") { 
				$.messager.alert('提示信息','提交成功','info');
				$('#editDlg').window('close');
				doQuery();
	        } else {
	        	$.messager.alert('提示信息','提交失败','info');
	        }
	    });
	    
	}else{
		$.messager.alert('提示信息','请输入模板组名称','info');
	}
	
}

//判断时间有效性
function chkInputDate(str){
	var reg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;  
	var r = str.match(reg); 
	if(r==null)return '';  
	r[2]=r[2]-1;  
	var d= new Date(r[1], r[2],r[3], r[4],r[5], r[6]);  
	if(d.getFullYear()!=r[1])return '';  
	if(d.getMonth()!=r[2])return '';  
	if(d.getDate()!=r[3])return '';  
	if(d.getHours()!=r[4])return '';  
	if(d.getMinutes()!=r[5])return '';  
	if(d.getSeconds()!=r[6])return '';  
	return str;  
}


//删除模板组
function delTmplGp(){
	var selectItem=$("#tmplSetGrid").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择模板组！','warning');
		return;
	}
	for(var i=0; i<selectItem.length; i++){
		if(Number(selectItem[i].publish||'0')==1){
			$.messager.alert('提示信息','不能删除已发布模板组！','warning');
			return;
		}
	}
	
	$.messager.confirm("确认",
		"是否确认[删除]所选模板组？<br/><span style='color:red'>*对应的[模板组/模版关联信息]将一并删除!</span>",
		function(r){
	        if (r) { 
				var ids = "";
				$.each(selectItem, function(index, item){
					ids = ids + item.ts_id + ",";
				});
				var url = appPath+"/itbTmplSet/delTmplGp.action?ids="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','删除成功','info');
						doQuery();
					}else{
						$.messager.alert('提示信息','删除失败','info');
					}
				});
	        }
		}
	);
}

// 发布模板组
function doPublish(){
	var selectTmplS=$("#tmplSetGrid").datagrid("getChecked");
	if(!selectTmplS || selectTmplS.length == 0){
		$.messager.alert('提示信息','请选择模板组处理！','warning');
		return;
	}else if(selectTmplS.length>1){
		$.messager.alert('提示信息','请选择单个模板组处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmplS, function(index, item){
		if(item.publish != 1 && item.publish != '1'){
			doFlg = true;
			ids = ids + item.ts_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择未发布的组进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认",
		"是否确认[发布]模板组？<br/><span style='color:red'>*请确保已经过认真检查!</span>",
		function (r) {
	        if (r) {
	        	var url = appPath+"/itbTmplSet/doTmplSPublish.action?ids="+ids;
	        	$.post(url,function(data){
	        		if(data=="ok"){
	        			$.messager.alert('提示信息','模板组发布成功！','info');
	        			doQuery();
	        		} else if (data=="ng"){
	        			$.messager.alert('提示信息','NG!','warning');
	        		}
	        	});
	        }
		}
	);
}

// 取消发布模板组
function cancelPublish(){
	var selectTmplS=$("#tmplSetGrid").datagrid("getChecked");
	if(!selectTmplS || selectTmplS.length == 0){
		$.messager.alert('提示信息','请选择已发布的模板组进行处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmplS, function(index, item){
		if(item.publish == 1 || item.publish == '1'){
			doFlg = true;
			ids = ids + item.ts_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择已发布的模板组进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认", "是否确认[取消]已发布模板组？", function (r) {
		if(r){
			var url = appPath+"/itbTmplSet/cancelTmplSPublish.action?ids="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','模板组已取消发布！','info');
					doQuery();
				}
			});
		}
	});
}

/** 显示顺序*/
function editOrder(tsId, order){
	$("#orderStId").val(tsId);
	$("#order").val(order);
	$("#orderBtn").show();
	$('#orderDlg').window({
		title:'显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#orderDlg').window('move',{top:20,left:20});
	$('#orderDlg').window('open');
}

/**修改显示顺序*/
function doUpdOrder(){
	var stId = $("#orderStId").val();
	var order = $("#order").val();
	if(!order || order.length == 0 || Number(order)>999 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～999','warning');
		return;
	}
	var url = appPath+"/itbTmplSet/updTmplSOrder.action?tsId="+stId+"&order="+order;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery();
				$('#orderDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}


/** 结束显示顺序编辑*/
function cancelUpdOrder(){
	$('#orderDlg').window('close');
}

/**获取请求信息
 * */
function getQueryParam(){
	var queryObject = {};
	var findUse = $("#queryTmplSUse").combobox('getValue');//用途
	var findPubFlg = $("#queryPublish").combobox('getValue');//发布状态
	var findTmplSTitle = $("#queryTmplTitle").val();
	var findTmplTsId = $("#queryTsId").val();
	var findDtS = $("#queryDateStart").datebox('getValue');
	var findDtE = $("#queryDateEnd").datebox('getValue');
	var cFindDtS = chkQueryDate(findDtS);
	var eFindDtS = chkQueryDate(findDtE);
	if(cFindDtS){
		queryObject.findDtS = cFindDtS;
	}
	if(eFindDtS){
		queryObject.findDtE = eFindDtS;
	}
	if(findUse && Number(findUse||'0')>0) queryObject.findUse = findUse;// 用途
	if(findPubFlg && Number(findPubFlg||'-1')>=0) queryObject.findPubFlg = findPubFlg;// 发布标记
	if(findTmplSTitle && findTmplSTitle.length>0) queryObject.findTmplSTitle = findTmplSTitle;// 名称
	if(findTmplTsId && findTmplTsId.length>0) queryObject.findTmplTsId = findTmplTsId;// 模板组ID
	return queryObject;
}

/**
 * Determine the validity of search time
 * @param dt
 * @returns {String}
 */
function chkQueryDate(dt){
	var strDate=dt+'';
    var ss = (strDate.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        var chkDt = new Date(y,m-1,d);
		if((chkDt.getFullYear()==y) &&((chkDt.getMonth()+1)==m) &&(chkDt.getDate()==d)) {
        	return dtFmt(chkDt);
        }
    }
    return '';
}

function dtFmt(dt){
	var y = dt.getFullYear();
    var m = dt.getMonth()+1;
    var d = dt.getDate();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
}

/** 查询数据*/
function doQuery(){
	$("#tmplSetGrid").datagrid('unselectAll');
	$("#tmplSetGrid").datagrid('load', getQueryParam());
}

/**查询活动商品*/
function doQuery2(startFlg){
	$("#tDesignGrid").datagrid('unselectAll');
	if(startFlg==1){
		$("#tDesignGrid").datagrid('load', getQueryParam());
	} else {
		$("#tDesignGrid").datagrid('reload', getQueryParam());
	}
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}

//加载模板组下的   设计模板列表
function initDesignGrid(){
	var tsId=$("#ts_id").val();
	$('#tDesignGrid').datagrid({
	    url:appPath + "/tmpSet/getTmplGroupDesignInfo.action?tsId="+tsId,
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'thumb',title:'缩略图',width:70,align:'center',sortable:false,
	        	formatter:function(thumb, rowData, rowIndex){
	        		var retUrl = '';
	        		if (rowData.thumb) {
	        			retUrl = '<a href="' + rowData.thumb + '" target="_blank">'
	        				+ '<div style="display:table;text-align:center;height:120px;width:100%">'
	        				+ '<span style="display:table-cell;vertical-align:middle">'
	        				+ '<img src="'+thumb+'?x-oss-process=image/resize,m_lfit,h_100,w_125'
	        				+ '" style="display:inline;max-height:100px;max-width:125px"></span></div></a>';
	        		}
	        		return retUrl;
	        	}
  	        },
	        {field:'design_id',title:'模版ID',width:35,align:'center',sortable:false,
		    	formatter:function(id, rowData, rowIndex){
		    		var map_id = rowData.map_id;
		    		var show = '<span style="color:red">'+id+'</span>';
		    		if(map_id){
			    		var show = '<span style="color:red">'+id+'</span>'+
			    		  	'</br>'+
			    		  	'<span style="color:green">'+rowData.map_id+'</span>';
		    		}
		    		return show;
		    	}
		    },
	        {field:'size',title:'尺寸(W*H)',width:55,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return ''+(rowData.width||'N/A')+
		    			'<span style="color:green">*</span>'+
		    			(rowData.height||'N/A')+'';
		    	}
		    },
		    {field:'top_type',title:'主分类',width:35,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var tn = getTopTypeNm(value);
		    		if(!tn || tn.length==0) {
		    			tn = '<span style="color:red">未知</span>';
		    		}
		    		return tn;
		    	}
		    },
	        {field:'type_name',title:'子分类',width:40,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return rowData.type_name || '<span style="color:red">未分类</span>';
		    	}
		    },
		    {field:'user_id',title:'设计师',width:50,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return rowData.nickname || rowData.user_name || rowData.user_id || '<span style="color:red">未知</span>';
		    	}
		    },
		    {field:'order',title:'顺序',width:35,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(!rowData.design_id) return "";
		    		  return "<a href='javascript:void(0)' onClick='editTSRorder("+rowData.design_id+","+rowData.order+")'>"+rowData.order+"</a>";
		    	  }
		    },
		    {field:'item_id',title:'关联商品',width:40,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var topType = Number(rowData.top_type||'0');
		    		if(topType!=4 && topType!=1010 && topType!=1011) {
		    			return '<span style="color:red">N/A</span>';
		    		}
		    		if(Number(value||'0')==0) {
		    			return '<span style="color:blue">未关联</span>';
		    		}
		    		return value;
		    	}
		    },
	        {field:'design_name',title:'模版名称',width:85,align:'left',sortable:false},
		    {field:'vip',title:'VIP',width:30,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  var show = "<span style='color:green;'>普通模板</span>";
		    		  if(Number(value||'0')==10) show="<span style='color:red;'>VIP模版</span>";
		    		  return show;
		    	  }
		    },
	        {field:'publish',title:'发布状态',width:40,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<span style="color:green">已发布</span>';
	        		return '<span style="color:red">未发布</span>';
	        	}
		    },
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:50,align:'center',sortable:false}
	    ]],
	    pagination:true,
		rownumbers:true,
		toolbar:[{
			text:'添加设计模板',
			iconCls:'icon-ok',
			handler:function(){
				addDesignInfo();
			}
		},
		{
			text:'移除设计模板',
			iconCls:'icon-cancel',
			handler:function(){
				delDesignInfo();
			}
		}]
	    
	});
}

//添加设计模板
function addDesignInfo(){
	$("#desBtn").show();
	$("#sDesignForm").form('clear');
	$('#sDesignDlg').window({
		title:'设计模板列表',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	// 大分类
	$("#queryTmplType").combobox({
		url:appPath+'/itbTg/getTypeForCombo.action?topIds=1,2,3,4,6,7,8,1010,1011&blkFlg=1',
		valueField:'type_id',
		textField:'type_name',
		onSelect:function(selRecd){
			$("#queryTmplType2").combobox("clear");
			$("#queryTmplType3").combobox("clear");
			$("#queryTmplType2").combobox("loadData",{});
			$("#queryTmplType3").combobox("loadData",{});
			// 中分类
			var parentId=selRecd.type_id;
			if(Number(parentId)>0){
				$("#queryTmplType2").combobox({
					url:appPath+'/itbTg/getTypeForCombo.action?parent='+parentId+'&blkFlg=1',
					valueField:'type_id',
					textField:'type_name',
					onSelect:function(selRecd2){
						$("#queryTmplType3").combobox("clear");
						$("#queryTmplType3").combobox("loadData",{});
						var parentId2=selRecd2.type_id;
						if(Number(parentId2)>0){
							$("#queryTmplType3").combobox({
								url:appPath+'/itbTg/getTypeForCombo.action?parent='+parentId2+'&blkFlg=1',
								valueField:'type_id',
								textField:'type_name'
							});
						}
					}
				});
			}
		}
	});
	$('#sDesignDlg').window('move',{top:0,left:20});
	$('#sDesignDlg').window('open');
	$("#queryVip").combobox({
		data:[{'id':"-1",'name':'不限'},{'id':"0",'name':'普通模板'},{'id':"10",'name':'VIP模版'}],
		valueField:'id',
		textField:'name',
		value:-1
	});
	initSuitDesign();
}

//移除设计模板
function delDesignInfo(){
	var selectItem=$("#tDesignGrid").datagrid("getChecked");
	var type = $("#stType").val();
	var stId = $("#ts_id").val();
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择一个设计模板！！','warning');
		return;
	}
	$.messager.confirm("确认","确认移除该设计模板？",function(r){
	        if (r) { 
				var ids = "";
				$.each(selectItem, function(index, item){
					ids= ids + item.design_id+",";
				});
				ids = ids.substring(0, ids.length-1);
				var url = appPath+"/tmpSet/delTmplSRDesign.action?tsId="+stId+"&design_id="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','移除成功','info');
						doQuery2();
					}else{
						$.messager.alert('提示信息','移除失败','info');
					}
				});
	        }
		}
	);
}

/**模板组下设计模板de顺序*/
function editTSRorder(designId, order){
	$("#tsrDesignId").val(designId);
	$("#tsrOrder").val(order);
	$("#tsrOrderBtn").show();
	$('#tsrOrderDlg').window({
		title:'模板组下模板顺序',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#tsrOrderDlg').window('move',{top:20,left:20});
	$('#tsrOrderDlg').window('open');
}

/**修改模板组下设计模板顺序
 * itb_tmpl_set_r
 * */
function upTsrOrder(){
	var designId = $("#tsrDesignId").val();
	var order = $("#tsrOrder").val();
	var tsId = $("#ts_id").val();
	if(!order || order.length == 0 || Number(order)>999 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～999','warning');
		return;
	}
	var url = appPath+"/tmpSet/updTsrOrder.action?designId="+designId+"&order="+order+"&tsId="+tsId;
	$.post(url,function(data){
		if(data=="ok"){
				$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery2();
				$('#tsrOrderDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}

function cancelUpdTOrder(){
	$('#tsrOrderDlg').window('close');
}

function getTopTypeNm(value){
	var tn = '';
	switch(Number(value||'0')){
  	case 1: tn='单页';break;
  	case 2: tn='多页';break;
  	case 3: tn='PPT';break;
  	case 4: tn='<span style="color:red">印品</span>';break;
  	case 6: tn='社交媒体';break;
  	case 7: tn='电商';break;
  	case 8: tn='微商';break;
  	case 1010: tn='Logo商品';break;
  	case 1011: tn='营销商品';break;
  }
  return tn;
}

//适合的设计模板信息
function initSuitDesign(){
	var tsId = $("#ts_id").val();
	$('#sDesignGrid').datagrid({
	    url:appPath + "/tmpSet/getSuitDesignInfo.action?tsId="+tsId,
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getQueryParam3(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'ts_id',title:'添加状态',width:35,align:'left',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		if(value){
	        			return '<span style="color:red">已添加</span>';
	        		}
	        		return '<span style="color:green;">未添加</span>';
	        	}
	        },
  	        {field:'thumb',title:'缩略图',width:60,align:'center',sortable:false,
	        	formatter:function(thumb, rowData, rowIndex){
	        		var retUrl = '';
	        		if (rowData.thumb) {
	        			retUrl = '<a href="' + rowData.thumb + '" target="_blank">'
	        				+ '<div style="display:table;text-align:center;height:95px;width:95px;">'
	        				+ '<span style="display:table-cell;vertical-align:middle">'
	        				+ '<img src="'+thumb+'?x-oss-process=image/resize,m_lfit,h_100,w_100'
	        				+ '" style="display:inline;max-height:95px;max-width:95px"></span></div></a>';
	        		}
	        		return retUrl;
	        	}
  	        },
	        {field:'design_id',title:'模版ID',width:35,align:'center',sortable:false,
		    	formatter:function(id, rowData, rowIndex){
		    		var show = '<span style="color:blue;">'+id+'</span>';
		    		if(rowData.map_id){
		    			show = '<span style="color:blue;">'+id+'</span>'+
		    			'</br>'+
		    			'<span style="color:green;">'+rowData.map_id+'</span>';
		    		}
		    		return show;
		    	}
		    },
	        {field:'size',title:'尺寸(W*H)',width:55,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return ''+(rowData.width||'N/A')+
		    			'<span style="color:green">*</span>'+
		    			(rowData.height||'N/A')+'';
		    	}
		    },
		    {field:'top_type',title:'主分类',width:35,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var tn = getTopTypeNm(value);
		    		if(!tn || tn.length==0) {
		    			tn = '<span style="color:red">未知</span>';
		    		}
		    		return tn;
		    	}
		    },
	        {field:'editType',title:'子分类',width:40,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return rowData.type_name || '<span style="color:red">未分类</span>';
		    	}
		    },
		    {field:'user_id',title:'设计师',width:50,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return rowData.nickname || rowData.user_name || rowData.user_id || '<span style="color:red">未知</span>';
		    	}
		    },
		    {field:'item_id',title:'关联商品',width:40,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var topType = Number(rowData.top_type||'0');
		    		if(topType!=4 && topType!=1010 && topType!=1011) {
		    			return '<span style="color:#FF3030;">N/A</span>';
		    		}
		    		if(Number(value||'0')==0) {
		    			return '<span style="color:blue">未关联</span>';
		    		}
		    		return value;
		    	}
		    },
	        {field:'design_name',title:'模版名称',width:70,align:'left',sortable:false},
		    {field:'vip',title:'VIP',width:35,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  var show = "<span style='color:green;'>普通模板</span>";
		    		  if(Number(value||'0')==10) show="<span style='color:red;'>VIP模版</span>";
		    		  return show;
		    	  }
		    },
	        {field:'publish',title:'发布状态',width:40,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<span style="color:green">已发布</span>';
	        		return '<span style="color:red">未发布</span>';
	        	}
		    },
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:50,align:'center',sortable:false}
	    ]],
	    pagination:true,
		rownumbers:true
	});
}

//添加设计模板  到分组下  添加到分组
function addToTmplSr(){
	var checkRows = $("#sDesignGrid").datagrid('getChecked');
	var tsId = $("#ts_id").val();
	if(checkRows.length==0){
		$.messager.alert('提示信息','请选择设计作品！','info');
		return;
	}else{
		var ids =[];
		var info = [];
		for(var i=0;i<checkRows.length;i++){
			var st = Number(checkRows[i].ts_id);//根据是否ts_id>0  判断是否已经添加过模板
			if(st<=0){
				var sts = {"design_id":checkRows[i].design_id,"top_type":checkRows[i].top_type};
				ids.push(sts);
			}else{
				info.push(checkRows[i].design_id);
			}
		}
		console.log(JSON.stringify(ids));
		if(info.length>0){
			$.messager.alert('提示信息','勾选的有已经添加过的模板设计！','info');
			return;
		}
		$.messager.confirm("确认","确定将该设计作品添加到该分组下？？",function(r){
	        if (r) { 
	        	var designIds = JSON.stringify(ids)
				var url = appPath+"/tmpSet/addSuitDesign.action";
	        	var data = {"tsId":tsId,"design_id":designIds};
				$.post(url,data,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','添加成功！','info');
						doQuery2();
						doQuery3();
					}else if(data=="exit"){
						$.messager.alert('提示信息','该模板组下已经存在该设计模板！！','info');
					}else if(data=="no"){
						$.messager.alert('提示信息','添加失败','info');
					}
				});
	        }
		});
	}
}

/**获取请求信息*/
function getQueryParam3(){
	var queryObject = {};
	var findTmplType =$("#queryTmplType3").combobox('getValue');
	if(!findTmplType){
		findTmplType =$("#queryTmplType2").combobox('getValue');
		if(!findTmplType){
			findTmplType=$("#queryTmplType").combobox('getValue');
		}
	}
	if (Number(findTmplType)>0) {
		queryObject.findTmplType = findTmplType;// 类型
	}
	var findVip = $("#queryVip").combobox('getValue');
	var findTmplName=$("#queryTmplName").val();
	var findTmplId=$("#queryTmplId").val();
	var findItemId=$("#queryItemId").val();
	if(findVip&&findVip.length>0&&Number(findVip)>=0) queryObject.findVip = findVip;// vip
	if(findTmplName&&findTmplName.length>0) queryObject.findTmplName = findTmplName;// 模版名称
	if(findTmplId&&findTmplId.length>0) queryObject.findTmplId = findTmplId;// 模版id
	if(findItemId&&findItemId.length>0) queryObject.findItemId = findItemId;// 商品id
	return queryObject;
}

/**查询数据*/
function doQuery3(startFlg){
	$("#sDesignGrid").datagrid('unselectAll');
	if(startFlg == 1) {
		$("#sDesignGrid").datagrid('load', getQueryParam3());
	} else {
		$("#sDesignGrid").datagrid('reload', getQueryParam3());
	}
}